﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class UserBLL : IBaseBLL
    {
        IBaseDAL<UserInfo> dal;
        public UserBLL(IBaseDAL<UserInfo> dal)
        {
            this.dal = dal;
        }
        public async Task<int> Add(UserInfo t)
        {
            try
            {
                var list = await dal.GetValue(x => x.Name == t.Name);
                if (list != null)
                {
                    return -1;
                }
                return await dal.GetAdd(t);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public IQueryable<UserInfo> GetShow(string? names = "", int? con = null, int? end = null, bool? sex = null)
        {
            try
            {
                var lisr = dal.GetAll();
                if (!string.IsNullOrEmpty(names))
                {
                    lisr=lisr.Where(x => x.Name.Contains(names));
                }
                if (con != null && end != null)
                {
                    lisr = lisr.Where(x => x.likes >= con && x.likes<=end);
                }
                if(sex!=null)
                {
                    lisr=lisr.Where(x=>x.Sex==sex);
                }
                return lisr;
            }
            catch (Exception)
            {

                throw;
            }
        }

        public async Task<dynamic> login(string username, string password)
        {
            var list=await dal.GetValue(x=>x.Name==username);
            if (list == null)
            {
                return -1;
            }
            else
            {
                if(list.Pwd!=password)
                {
                    return list;
                }
                else
                {
                    return -2;
                }
            }
        }
    }
}
